package com.wantup.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * <p>
 *
 * </p>
 *
 * @author 赵洛伟
 * @since 2019-06-05
 * <p>
 * 第二数据库的配置信息
 */
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.wantup.multi_datasource2", sqlSessionFactoryRef = "test2SqlSessionFactory")
@Slf4j
public class DataSource2Config {
    @Value("${spring.datasource.test2.jdbc-url}")
    private String dbUrl;

    @Value("${spring.datasource.test2.username}")
    private String username;

    @Value("${spring.datasource.test2.password}")
    private String password;

    @Value("${spring.datasource.test2.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.test2.borrowConnectionTimeout}")
    private int borrowConnectionTimeout;

    @Value("${spring.datasource.test2.loginTimeout}")
    private int loginTimeout;

    @Value("${spring.datasource.test2.maintenanceInterval}")
    private int maintenanceInterval;

    @Value("${spring.datasource.test2.maxIdleTime}")
    private int maxIdleTime;

    @Value("${spring.datasource.test2.maxLifetime}")
    private int maxLifetime;

    @Value("${spring.datasource.test2.maxPoolSize}")
    private int maxPoolSize;

    @Value("${spring.datasource.test2.minPoolSize}")
    private int minPoolSize;


    /**
     * 配置第一个数据库
     *
     * @return
     */
    @Bean(name = "test2DataSource")
    public DataSource testDataSource() throws SQLException {
//        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
//        mysqlXaDataSource.setUrl(dbUrl);
//        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//        mysqlXaDataSource.setPassword(password);
//        mysqlXaDataSource.setUser(username);
//        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
//


        Properties properties = new Properties();
        properties.setProperty("URL", dbUrl);
        properties.setProperty("user", username);
        properties.setProperty("password", password);
        log.info("URL --> " + dbUrl + " user --> " + username + " password --> " + password);

        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        //xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setXaProperties(properties);
        xaDataSource.setUniqueResourceName("test2DataSource");
        xaDataSource.setUniqueResourceName("OracleXADataSource");
        xaDataSource.setXaDataSourceClassName("oracle.jdbc.xa.client.OracleXADataSource");
        xaDataSource.setMinPoolSize(minPoolSize);
        xaDataSource.setMaxPoolSize(maxPoolSize);
        xaDataSource.setMaxLifetime(maxLifetime);
        xaDataSource.setBorrowConnectionTimeout(borrowConnectionTimeout);
        xaDataSource.setLoginTimeout(loginTimeout);
        xaDataSource.setMaintenanceInterval(maintenanceInterval);
        xaDataSource.setMaxIdleTime(maxIdleTime);


        return xaDataSource;
    }

    /**
     * 创建 第一个数据库的 SessionFactory
     *
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // bean.setMapperLocations(
        // new
        // PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}
